/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-07-18 08:56:07
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-01-04 11:00:16
 * @FilePath: \ems_neutral_app\lib\pages\Mine\Serve\ServePage.dart
 * @Description:我的服务
 */

import 'package:ems_neutral_app/base/SilAppBar.dart';
import 'package:ems_neutral_app/base/SilScaffold.dart';
import 'package:ems_neutral_app/common/widgets/cubitWidget/DividingLine.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/pages/Mine/MinePage/MineController.dart';
import 'package:ems_neutral_app/utils/color/ColorUtils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../config/AppInfo.dart';

class ServePage extends StatefulWidget {
  const ServePage({super.key});

  @override
  State<ServePage> createState() => _ServePageState();
}

class _ServePageState extends State<ServePage> {
  MineController _mineController = Get.put(MineController());

  @override
  void initState() {
    super.initState();

    _mineController.getCompanyInfo(context: context);
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
      sideMargin: 0,
      stateBarHide: true,
      navBarHide: true,
      backgroundColor: ColorUtils.hexColor('#0e0e0e'),
      body: mainWidget(),
    );
  }

  mainWidget() {
    return Container(
      margin: EdgeInsets.only(top: 0.w),
      decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage(AppInfo.stationType == '3'
                  ? 'imgs/set/set_bg_e3.png'
                  : 'imgs/set/set_bg.png'),
              fit: BoxFit.fitWidth,
              alignment: Alignment.topCenter)),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Column(
            children: [
              topNavView(),

              /// 手机号
              phoneTitle(),

              /// 营业网点
              // outletInformationView(),
            ],
          ),

          /// 其他信息
          elseInfoView()
        ],
      ),
    );
  }

  /// 顶部信息
  topNavView() {
    return Container(
      margin: EdgeInsets.only(
          top: MediaQuery.of(context).padding.top, bottom: 30.w),
      child: SilAppBar(
        backgroundColor: Colors.transparent,
        leadingWidget: SilScaffoldButton.backButtonStatic(context),
        titleWidget: SilScaffoldButton.titleView(
            title: LangOf(context)?.bottom?.service),
      ),
    );
  }

  /// 电话号码
  phoneTitle() {
    String phone = '+86 ${kVersionInfo['contactPhone']}';
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          SilImage(
            'imgs/personal/personal_phone_icon.png',
            width: 46.w,
            height: 46.w,
          ),
          Padding(padding: EdgeInsets.only(right: 10.w)),
          SilText(
            phone,
            fontSize: 24.sp,
            color: ColorUtils.color_font_main,
          )
        ],
      ),
    );
  }

  /// 网点信息
  outletInformationView() {
    return Container(
      margin: EdgeInsets.only(top: 120.w, right: 20.w),
      alignment: Alignment.topRight,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          outletInformationViewItem(
              imgPath: 'imgs/personal/personal_place1.png',
              value: '${LangOf(context)?.concactUs?.commercial_networks}：12'),
          outletInformationViewItem(
              imgPath: 'imgs/personal/personal_place2.png',
              value: '${LangOf(context)?.concactUs?.after_networks}：8'),
          outletInformationViewItem(
              imgPath: 'imgs/personal/personal_place3.png',
              value: '${LangOf(context)?.concactUs?.outlets}：3'),
        ],
      ),
    );
  }

  outletInformationViewItem({String? imgPath, String? value}) {
    return UnconstrainedBox(
        child: Container(
      margin: EdgeInsets.only(bottom: 5.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SilImage(imgPath, width: 15.w, height: 15.w),
          Padding(padding: EdgeInsets.only(right: 5.w)),
          SilText(
            value,
            color: Colors.white,
            fontSize: 10.sp,
            maxLines: 0,
          ),
        ],
      ),
    ));
  }

  /// 其他信息
  elseInfoView() {
    return Container(
      child: Stack(
        alignment: AlignmentDirectional.topCenter,
        children: [
          Container(
            // height: 392.w,
            height: MediaQuery.of(context).size.height - 360.w,
          ),

          /// 其他信息列表
          Positioned(
              child: Obx(() => listView()),
              top: 30.w,
              left: 0,
              right: 0,
              bottom: 0),

          /// 联系我们按钮
          Positioned(
              child: Container(
                child: contactUsView(),
              ),
              top: 10.w),
        ],
      ),
    );
  }

  /// 联系我们
  contactUsView() {
    return Container(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          contactUsBtns(
            title: LangOf(context)?.concactUs?.tel,
            imgPath: 'imgs/personal/personal_phone.png',
            onTapFunc: () async {
              String phoneNumber =
                  _mineController.companyEntity.value.phone ?? '18773194059';
              final url = Uri.parse('tel:+86 ' + phoneNumber);
              if (await canLaunchUrl(url)) {
                await launchUrl(url);
              } else {
                print('拨打电话失败！');
              }
            },
          ),
          Padding(padding: EdgeInsets.only(right: 20.w)),
          contactUsBtns(
            title: LangOf(context)?.concactUs?.IM,
            imgPath: 'imgs/personal/personal_icon2.png',
            onTapFunc: showWhatsappPopView,
          ),
        ],
      ),
    );
  }

  /// 联系我们按钮
  contactUsBtns({String? title, String? imgPath, void Function()? onTapFunc}) {
    return SilEmptyButton(
      useConstrained: false,
      child: Container(
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(23.w),
            gradient: LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [
                  ColorUtils.hexColor('#6bc6d8'),
                  ColorUtils.hexColor('#268496'),
                ])),
        padding: EdgeInsets.symmetric(horizontal: 25.w, vertical: 10.w),
        child: SilButton(
          title: title,
          titleColor: ColorUtils.color_font_main,
          fontSize: 16.sp,
          imagePosition: ImagePosition.Left,
          space: 5.w,
          imgPath: imgPath,
          imgHeight: 24.w,
          imgWidth: 24.w,
        ),
      ),
      onTapFunc: onTapFunc,
    );
  }

  listView() {
    return Container(
        // height: 362.w,
        padding: EdgeInsets.only(left: 22.w, right: 22.w, top: 50.w),
        decoration: BoxDecoration(
            color: ColorUtils.hexColor('#cfcfcf'),
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(15.w),
                topRight: Radius.circular(15.w))),
        child: ListView(
          children: [
            listItem(
                imgPath: 'imgs/personal/personal_i1.png',
                value: LangOf(context)?.concactUs?.name),
            listItem(
                imgPath: 'imgs/personal/personal_i2.png',
                value: LangOf(context)?.concactUs?.address),
            listItem(
                imgPath: 'imgs/personal/personal_i3.png',
                value: _mineController.companyEntity.value.phone),
            listItem(
                imgPath: 'imgs/personal/personal_i4.png',
                value: _mineController.companyEntity.value.email),
            listItem(
                imgPath: 'imgs/personal/personal_i5.png', value: 'cnlenercom'),
            listItem(
                imgPath: 'imgs/personal/personal_i6.png',
                value: _mineController.companyEntity.value.website),
          ],
        ));
  }

  listItem({String? imgPath, String? value}) {
    return Container(
      margin: EdgeInsets.only(bottom: 20.w),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SilImage(imgPath, width: 17.w, height: 17.w),
          Padding(padding: EdgeInsets.only(right: 10.w)),
          Expanded(
              child: SilText(
            value,
            color: Colors.black,
            fontSize: 14.sp,
            maxLines: 0,
          )),
        ],
      ),
    );
  }

  /// 显示Whatsapp联系我们
  showWhatsappPopView() {
    String whatsappNum = _mineController.companyEntity.value.skypeNo ?? "";
    SilBottomSheet.show(
        context: context,
        child: Container(
          height: 400.w,
          decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                  topLeft: Radius.circular(10.w),
                  topRight: Radius.circular(10.w))),
          child: Column(
            children: [
              Container(
                height: 64.w,
                alignment: Alignment.center,
                child: SilText(
                  LangOf(context)?.skype?.skypetitle,
                  fontSize: 14.sp,
                  color: Colors.black,
                ),
              ),
              DividingLine(
                color: ColorUtils.hexColor('#e4e4e4'),
                margin: EdgeInsets.only(bottom: 20.w),
              ),
              Row(
                children: [
                  Padding(padding: EdgeInsets.only(left: 20.w)),
                  SilText(
                    'Whatsapp:${whatsappNum}',
                    fontSize: 14.sp,
                    color: Colors.black,
                  ),
                  SilButton(
                    margin: EdgeInsets.only(left: 20.w),
                    title: LangOf(context)?.concactUs?.copyText,
                    fontSize: 14.sp,
                    titleColor: Colors.white,
                    backgroundColor: ColorUtils.color_theme,
                    radius: 15.w,
                    padding:
                        EdgeInsets.symmetric(horizontal: 20.w, vertical: 7.5.w),
                    onTapFunc: () {
                      /// 复制到粘贴板
                      Clipboard.setData(ClipboardData(text: whatsappNum));
                      String? msg = LangOf(context)?.concactUs?.copy;
                      SilToast.show(msg ?? "");
                    },
                  )
                ],
              )
            ],
          ),
        ));
  }
}
